package com.pinc.poop;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdView;

public class WidgetActivity extends Activity implements AdListener{
	protected ImageView tv_right;
	protected ImageView tv_left;
	protected ImageView img_right;
	protected TextView tv_title;
	protected AdView adview;
	protected SharedPreferences settings;

	protected void initWidget() {
		/* init upbar */
		tv_right = (ImageView) this.findViewById(R.id.imageButton1);
		tv_left = (ImageView) this.findViewById(R.id.imageButton2);
		img_right = (ImageView) this.findViewById(R.id.imageButton3);
		tv_title = (TextView) this.findViewById(R.id.textView_title);

		img_right.setVisibility(View.GONE);

		adview = (AdView) this.findViewById(R.id.adView);
		adview.setAdListener(this);
	}

	protected void onResume() {
		super.onResume();
		loadAD();
	}

	protected void loadAD()
	{
		adview.setVisibility(View.GONE);
		loadADwoGone();
	}

	protected void loadADwoGone()
	{
		AdRequest request = new AdRequest();
		settings = PreferenceManager.getDefaultSharedPreferences(this);
		String user_sex_str = settings.getString("user_sex", "null");
		if ( user_sex_str.compareTo(getResources().getStringArray(R.array.str_arr_gender)[0]) == 0)
		{
			request.setGender(AdRequest.Gender.FEMALE);
		}
		else if( user_sex_str.compareTo(getResources().getStringArray(R.array.str_arr_gender)[1]) == 0)
		{
			request.setGender(AdRequest.Gender.MALE);
		}
		adview.loadAd(request);
	}

	@Override
	public void onDismissScreen(Ad arg0) {
	}

	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
	}

	@Override
	public void onLeaveApplication(Ad arg0) {
	}

	@Override
	public void onPresentScreen(Ad arg0) {
	}

	@Override
	public void onReceiveAd(Ad arg0) {
		adview.setVisibility(View.VISIBLE);
	}
}
